package com.windupurnomo.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.windupurnomo.entity.Player;
import com.windupurnomo.util.D;
import com.windupurnomo.util.DataGenerator;
import com.windupurnomo.util.TableGenerator;

public class ActivityManagerData extends SQLiteOpenHelper{
	private static final String DATABASE_NAME = "ActivityManager";

	public ActivityManagerData(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
	
	public void createTable(SQLiteDatabase db){
		db.execSQL("DROP TABLE IF EXISTS "+ D.table.TASK);
		db.execSQL("DROP TABLE IF EXISTS "+ D.table.CONDITION);
		db.execSQL("DROP TABLE IF EXISTS "+ D.table.RESOURCE);
		db.execSQL("DROP TABLE IF EXISTS "+ D.table.DAY);
		db.execSQL("DROP TABLE IF EXISTS "+ D.table.GAME);
		db.execSQL("DROP TABLE IF EXISTS "+ D.table.TASK_DAY);
		Log.i("acmWindu", "Drop tables");
		
		db.execSQL(TableGenerator.TASK);
		db.execSQL(TableGenerator.PLAYER);
		db.execSQL(TableGenerator.CONDITION);
		db.execSQL(TableGenerator.RESOURCE);
		db.execSQL(TableGenerator.DAY);
		db.execSQL(TableGenerator.GAME);
		db.execSQL(TableGenerator.TASK_DAY);
		db.execSQL(TableGenerator.GAME_DAY);
		db.execSQL(TableGenerator.PLAYER_CONDITION);
		db.execSQL(TableGenerator.PLAYER_RESOURCE);
		db.execSQL(TableGenerator.TASK_RESOURCE);
		db.execSQL(TableGenerator.TASK_CONDITION);
		Log.i("acmWindu", "Generate tables");
	}
	
	public void genDataResource(SQLiteDatabase db){
		DataGenerator.genResourceData(db);
		DataGenerator.genConditionData(db);
		DataGenerator.genTaskData(db);
		DataGenerator.genDayData(db);
		DataGenerator.genTaskDayData(db);
		DataGenerator.genGameData(db);
		Log.i("acmWindu", "Generate data");
	}
	
	public long generateData(SQLiteDatabase db, Player player){
		ContentValues cv = new ContentValues();
		cv.put("name", player.getName());
		cv.put("sex", player.getSex());
		return db.insert(D.table.PLAYER, "ccc", cv);
	}

}
